package cn.dodgeball.java;


import cn.dodgeball.util.Const;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SetSlider extends JPanel {
   private String type = null;
    JTextField valueText = new JTextField(); // 数值文本框
    JSlider slider = null ;

    public SetSlider(String name,int num,int min,int max,int y){
        type = name;

        setLayout(null);                      // 自定义布局
        setBounds((int)((Const.FRAME_WIDTH-800)/2),y,800,120);  // 位置和大小
        setOpaque(false);                     // 透明背景

        // 上区域
        JPanel upPanel = new JPanel(null);
        upPanel.setBounds(260,0,280,60);
        upPanel.setOpaque(false);
        // 设置文字
        JLabel colorText = new JLabel(name);
        colorText.setFont(Const.SETFONT);
        colorText.setForeground(Color.yellow);
        colorText.setBounds(10,0,200,60);
        upPanel.add(colorText);
        // 文本框
        valueText.setText(String.valueOf(num));
        valueText.setEditable(false);
        valueText.setFont(Const.SETFONT);
        valueText.setBounds(200,0,70,60);
        valueText.setOpaque(false);
        valueText.setForeground(Color.yellow);
        valueText.setBorder(null);
        upPanel.add(valueText);
        this.add(upPanel);

        //下区域
        JPanel downPanel = new JPanel(null);
        downPanel.setBounds(0,60,800,60);
        downPanel.setOpaque(false);
        // 滑块
        slider = new JSlider(min,max,num);
        slider.setBounds(60,0,680,60);
        slider.setOpaque(false);
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                update( slider.getValue());
            }
        });
        downPanel.add(slider);
        // 减少
        JButton minus = new JButton("-");
        minus.setBounds(0,0,60,60);
        minus.setFont(new Font("宋体",Font.BOLD,100));
        minus.setForeground(Color.yellow);
        minus.setContentAreaFilled(false);
        minus.setBorder(null);
        minus.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                update(slider.getValue()-1);
            }
        });
        downPanel.add(minus);
        // 增加
        JButton plus = new JButton("+");
        plus.setBounds(740,0,60,60);
        plus.setFont(new Font("宋体",Font.BOLD,100));
        plus.setForeground(Color.yellow);
        plus.setContentAreaFilled(false);
        plus.setBorder(null);
        plus.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                update(slider.getValue()+1);
            }
        });
        downPanel.add(plus);

        this.add(downPanel);
    }

    private void update(int val){
        switch (type){
            case "方块大小":
                Const.squareRadius = val;
                break;
            case "小球数量":
                Const.ballAccount = val;
                break;
            case "小球大小":
                Const.ballRadius = val;
        }
        valueText.setText(String.valueOf(val));
        slider.setValue(val);
//        System.out.print(val);
//        System.out.print('\n');
    }
}
